package com.example.whm;



import java.util.Map;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Application;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.widget.ListView;
import org.holoeverywhere.widget.Toast;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.example.whm.MyPreOrderFragment.adapter;
import com.example.whm.model.Carousel;
import com.example.whm.model.GalleryModel;
import com.example.whm.model.PreOrder;
import com.example.whm.model.ZaoDian;

public class OneFragmentGallery extends Fragment implements OnClickListener{

	private AQuery aq;
	private AQuery aq2;
	
	private String topic;
	GalleryModel g;
	
	
	
	

    @Override
 	public View onCreateView(LayoutInflater inflater, ViewGroup container,
 			Bundle savedInstanceState) {
     	View view = inflater.inflate(R.layout.one_layout_gallery, container, false); 
     	System.out.println("=======================1");
     	g=(GalleryModel) getArguments().getSerializable("gallery");
     	aq2=new AQuery(getActivity(), view);
     	return view;
 	}
    
    



	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		System.out.println("=======================1");
	
	 //  init();
		refresh();
	}
	
	
public void refresh(){
	if (g!=null) {
		String tbUrl = g.getFirst().getPre_pic().replace("localhost", Whm_app.api_url);
	    //System.out.println(tbUrl);
		aq2.id(R.id.one_layout_gallery_imageview).getImageView().setTag(g.getFirst());
		aq2.id(R.id.one_layout_gallery_imageview).clicked(OneFragmentGallery.this);
		aq2.id(R.id.one_layout_gallery_imageview).image(tbUrl, true, true, 200, R.drawable.ic_launcher, null,0, 0);
	
		
		if (g.getLast()!=null) {
			String tbUrl2 = g.getLast().getPre_pic().replace("localhost", Whm_app.api_url);
			aq2.id(R.id.one_layout_gallery_imageview2).getImageView().setTag(g.getLast());
			aq2.id(R.id.one_layout_gallery_imageview2).clicked(OneFragmentGallery.this);
			aq2.id(R.id.one_layout_gallery_imageview2).image(tbUrl2, true, true, 200, R.drawable.ic_launcher, null,0, 0);
		}
	}
	
}
	
	
	

	ListView list;
public void init(){
	
	  String url="http://"+Whm_app.api_url+":9000/api/user_preorder";
	  AjaxCallback<String> cb = new AjaxCallback<String>();        
	  cb.url(url).type(String.class).weakHandler(this, "ui");
	  SharedPreferences sp=((Application)getActivity().getApplication()).getSharedPreferences("whm",Context.MODE_PRIVATE);
  	String cookiename=sp.getString("cookie_name",null);
  	String cookievalue=sp.getString("cookie_value",null);
  	if (cookiename!=null&&cookievalue!=null) {
  		cb.cookie(cookiename,cookievalue);               
	}
	  aq.ajax(cb);
  }

  public void ui(String url, String r, AjaxStatus status){
	
	  System.out.println("ui  s="+r); 
	  if (status.getCode()==200) {
		 
		  
		  final String s=r;
		  new Thread( new Runnable() {
			  @Override
			public void run() {
				  // String s1= JSON.parseObject(s).getString("users");
				  
				/*  String mapr= JSON.parseObject(s).getString("mapr");
				  String listj= JSON.parseObject(s).getString("list");
				  map= JSON.parseObject(mapr, Map.class);
				  
				  boxs= JSON.parseArray(listj, PreOrder.class);
				  System.out.println("boxs.size()========="+boxs.size());*/
				
				  getActivity().runOnUiThread(new Runnable() {
					  
					  @Override
					  public void run() {
						 /* Toast.makeText(getActivity(), "boxs.size()="+boxs.size(), 1000).show();
						  if (isload) {
							  show_dialog(false);
							 
							  adap.setList(boxs);
							  adap.notifyDataSetChanged();
						}else{		
							isload=true;
						  aq2.id(R.id.progress).gone();
						//  aq2.id(R.id.four_layout_line).visible();
						  
						//   list=(ListView) aq2.id(R.id.myorder_fragment_listview).getListView();
						  View v=getActivity().getLayoutInflater().inflate(R.layout.four_layout_header, null);
						  View v2=getActivity().getLayoutInflater().inflate(R.layout.four_layout_footer, null);
						   AQuery a=new AQuery(v);
						  AQuery a2=new AQuery(v2);
						  a.id(R.id.four_layout_contact_name).text(add.getContact_name());
						  a.id(R.id.four_layout_contact_phone).text(add.getPhone());
						  a.id(R.id.four_layout_contact_addr).text(add.getDetail_address());
						  a2.find(R.id.four_layout_btn_sure).clicked(MyOrderFragment.this); 
						   radioButtonGroup=(RadioGroup) a.find(R.id.four_layout_pay).getView();
						  list.addHeaderView(v);
						  list.addFooterView(v2);
						 
						  adap=new adapter(boxs);
						  aq2.id(R.id.myorder_fragment_listview).adapter(adap);
						  aq2.id(R.id.myorder_fragment_listview).itemClicked(MyPreOrderFragment.this);
						  aq2.id(R.id.myorder_fragment_listview).getListView().setOnItemLongClickListener(MyPreOrderFragment.this);
						
						
						
						
						}*/
						  
						  
						  
						  
						  
						  
						  
						  
						  
						  
						  
						  //Utility.setListViewHeightBasedOnChildren(list);
					  }
				  });
			  }
		  }).start();
			
		  
	}else{
		Toast.makeText(getActivity(), "request error", 1000).show();
		
	}
	  
  }





@Override
public void onClick(View v) {
	// TODO Auto-generated method stub
	ZaoDian z=(ZaoDian) v.getTag();
	 Intent inte=new Intent(getActivity(), EntityDetail.class);
	  if (inte.getExtras()!=null) {
		  inte.getExtras().putString("id", z.getId());
	}else{
		Bundle b=new Bundle();
		b.putString("id", z.getId());
		inte.putExtras(b);
	}
	  startActivity(inte);
	
}


    
    
    
    
    
    
    
    
    
    
 
}
